<template>
  <!--案件管理-回收案件池-->
  <section class="page recovered-case">
    <data-form :model="importModel" @onSearch="refreshData" :page="pageService" :rules="recoveredRule" @handleResetForm="follFeedback">
      <template slot="inputs">
        <el-form-item label="订单号" prop="caseNumber">
          <el-input v-model="importModel.caseNumber"></el-input>
        </el-form-item>
        <el-form-item label="批次号" prop="batchNumber">
          <el-input v-model="importModel.batchNumber"></el-input>
        </el-form-item>
        <el-form-item label="委托方" prop="principalId">
          <el-select v-model="importModel.principalId" placeholder="请选择" clearable>
            <el-option v-for="{index,name,id} in principalList.content" :key="index" :label="name" :value="id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="客户姓名" prop="personalName">
          <el-input v-model="importModel.personalName"></el-input>
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard">
          <el-input v-model="importModel.idCard"></el-input>
        </el-form-item>
        <el-form-item label="放款日期" prop="loanDate">
          <el-date-picker type="daterange" :editable="false" v-model="importModel.loanDate" placeholder="选择日期时间" clearable>
          </el-date-picker>
        </el-form-item>
        <el-form-item label="手机号" prop="mobileNo">
          <el-input v-model="importModel.mobileNo" :maxlength="11"></el-input>
        </el-form-item>
        <el-form-item label="业务团队" prop="partnerName">
          <el-input v-model="importModel.partnerName"></el-input>
        </el-form-item>
        <el-form-item label="贷款银行" prop="bankName">
          <el-input v-model="importModel.bankName"></el-input>
        </el-form-item>
        <el-form-item label="催收总金额" prop="sumMoney">
          <number-range v-model="importModel.amountRange"></number-range>
        </el-form-item>
        <el-form-item label="业务承接地" prop="businessAddress">
          <el-input v-model="importModel.businessAddress"></el-input>
        </el-form-item>
        <el-form-item label="上牌地" prop="boardAddress">
          <el-input v-model="importModel.boardAddress"></el-input>
        </el-form-item>
        <el-form-item label="车牌号" prop="carNo">
          <el-input v-model="importModel.carNo"></el-input>
        </el-form-item>
        <el-form-item label="案件来源" prop="dataFromSource">
          <el-select v-model="importModel.dataFromSource" placeholder="请选择" clearable>
            <el-option v-for="{index,name,code} in $dict.getDictData('DataFromSource')" :key="index" :label="name" :value="code"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分案时间" prop="partDistributeDate">
          <el-date-picker type="datetimerange" :editable="false" v-model="importModel.partDistributeDate" placeholder="选择日期时间" clearable>
          </el-date-picker>
        </el-form-item>
        <el-form-item label="部门回收时间" prop="returnDate">
          <el-date-picker type="datetimerange" :editable="false" v-model="importModel.returnDate" placeholder="选择日期时间" clearable>
          </el-date-picker>
        </el-form-item>
        <el-form-item label="回收方式" prop="recoveryType">
          <el-select v-model="importModel.recoveryType" placeholder="请选择" clearable>
            <el-option v-for="{index,name,code} in $dict.getDictData('RecoveryType')" :key="index" :label="name" :value="code"></el-option>
          </el-select>
        </el-form-item>
      </template>
    </data-form>
    <data-box :data="importDataSet" :selectionList.sync="selectionList" @onPageChange="refreshData" :page="pageService">
      <template slot="buttons">
        <el-button @click="manualDistruibute" v-auth="2314">分案</el-button>
        <el-button @click="exportCaseInfo">案件导出</el-button>
        <el-button @click="exportFollowup">催记导出</el-button>
        <el-button @click="updateRecoveryType" v-auth="2315">更新回收方式</el-button>
      </template>
      <template slot="columns">
        <!--数据列区域-->
        <el-table-column prop="caseInfo.caseNumber" label="订单编号" :min-width="$helper.getColumnWidth(6)">
          <template slot-scope="scope">
            <el-button type="text" @click="openExecutionPage(scope.row)">{{scope.row.caseNumber}}</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="batchNumber" label="批次号" :min-width="$helper.getColumnWidth(6)">
        </el-table-column>
        <el-table-column prop="principalName" label="委托方" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="personalName" label="客户姓名" :min-width="$helper.getColumnWidth(3)">
        </el-table-column>
        <el-table-column prop="idCard" label="身份证号" :min-width="$helper.getColumnWidth(6)">
        </el-table-column>
        <el-table-column prop="loanDate" label="放款日期" :formatter="(row) => $filter.dateFormat(row.loanDate)" :min-width="$helper.getColumnWidth(5)">
        </el-table-column>
        <el-table-column prop="mobileNo" label="手机号" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="partnerName" label="业务团队" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="bankName" label="贷款银行" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="loanAmount" label="贷款金额" :min-width="$helper.getColumnWidth(4)" :formatter="(row) =>  $filter.toThousands(row.loanAmount)">
        </el-table-column>
        <el-table-column prop="overdueStage" label="逾期阶段" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="overdueDays" label="逾期天数" :min-width="$helper.getColumnWidth(3)">
        </el-table-column>
        <el-table-column prop="sumMoney" label="催收总金额" :min-width="$helper.getColumnWidth(6)" :formatter="(row) =>  $filter.toThousands(row.overdueAmount)">
        </el-table-column>
        <el-table-column prop="businessAddress" label="业务承接地" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="boardAddress" label="上牌地" :min-width="$helper.getColumnWidth(3)">
        </el-table-column>
        <el-table-column prop="carNo" label="车牌号" :min-width="$helper.getColumnWidth(3)">
        </el-table-column>
        <el-table-column prop="dataFromSource" label="案件来源" :formatter="(row) => $filter.dictConvert(row.dataFromSource,'DataFromSource')" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="partDistributeDate" label="分案时间" :formatter="(row) => $filter.dateTimeFormat(row.partDistributeDate)" :min-width="$helper.getColumnWidth(5)">
        </el-table-column>
        <el-table-column prop="returnDate" label="回收时间" :formatter="(row) => $filter.dateTimeFormat(row.returnDate)" :min-width="$helper.getColumnWidth(5)">
        </el-table-column>
        <el-table-column prop="performanceRate" label="绩效比例(%)" :min-width="$helper.getColumnWidth(4)" :formatter="(row) =>  $filter.toThousands(row.performanceRate)">
        </el-table-column>
        <el-table-column prop="recoveryType" label="回收方式" :formatter="(row) => $filter.dictConvert(row.recoveryType,'RecoveryType')" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="operatorTime" label="状态变更时间" :formatter="(row) => $filter.dateTimeFormat(row.operatorTime)" :min-width="$helper.getColumnWidth(5)">
        </el-table-column>
        <el-table-column prop="operatorRealName" label="回收操作人" :min-width="$helper.getColumnWidth(5)">
        </el-table-column>
      </template>
    </data-box>
    <!--案件分配弹框-->
    <el-dialog title="案件分配" :center="true" :visible.sync="dialog.manual" width="600px" @open="$nextTick(()=>$refs['manual-distrubute'].refresh(selectionList,importModel))">
      <manual-distrubute ref="manual-distrubute" @close="dialog.manual=false" @refreshList="refreshData" :selectionList="selectionList" :importModel="importModel"></manual-distrubute>
    </el-dialog>
  </section>
</template>

<script lang="ts">
import Vue from "vue";
import Component from "vue-class-component";
import { Dependencies, Layout, Auth } from "~/core/decorator";
import NumberRange from "~/components/common/number-range.vue";
import { PageService } from "~/utils/page.service";
import ManualDistrubute from "~/components/case-import/manual-distrubute.vue";
import { namespace, State } from "vuex-class";
import { CaseInfoService } from "../../services/domain-service/case-info.service";
import { CASE_CHANNEL, MENU_CHANNEL } from "~/config/enum.config";
import GetLabelName from "~/components/labeling-strategy/get-label-name.vue";

const CollectionExecutionModule = namespace("collection-execution");

@Auth(2313)
@Layout("workspace")
@Component({
  components: {
    ManualDistrubute,
    GetLabelName,
    NumberRange
  }
})
export default class RecoveredCase extends Vue {
  @State
  public principalList: any;
  @Dependencies(PageService)
  private pageService: PageService;
  @Dependencies(CaseInfoService)
  private CaseInfoService: CaseInfoService;
  private importModel: any = {
    principalId: "",
    batchNumber: "",
    mobileNo: "",
    personalName: "",
    amountRange: { min: "", max: "" },
    caseNumber: '',
    principalName: '',
    partnerName: '',
    bankName: '',
    businessAddress: '',
    boardAddress: '',
    carNo: '',
    idCard: '',
    loanDate: [],
    partDistributeDate: [],
    returnDate: [],
    dataFromSource: '',
    recoveryType: ''
  };
  private dialog: any = {
    manual: false
  };

  // 表单验证规则
  private recoveredRule = {
    mobileNo: [
      {
        validator: this.$validator.phoneNumber,
        trigger: "blur"
      }
    ]
  };

  private importDataSet: any = [];
  private selectionList: any[] = [];
  private caseDetail: any = {};
  /**
   * 获取回收案件
   */
  public refreshData() {
    this.CaseInfoService.getAllRecoveredCaseInfos(
      this.importModel,
      this.pageService
    ).subscribe(data => {
      this.importDataSet = data;
      this.selectionList = [];
    });
  }

  private mounted() {
    this.refreshData();
  }

  /**
   * 手动分案
   */
  private manualDistruibute() {
    // if (this.selectionList.length === 0) {
    // this.$message("请选择要分配的案件");
    this.dialog.manual = true;
    // } else {
    //     this.dialog.manual = true;
    // }
  }

  /**
   * 重置numberRange
   */
  private follFeedback() {
    this.importModel.amountRange = { min: "", max: "" };
    this.importModel.dayRange = { min: "", max: "" };
    this.importModel.rateRange = { min: "", max: "" };
    this.importModel.caseMoney = { min: '', max: '' }
  }

  /**
   * 进入查看执行页
   */
  private openExecutionPage(row) {
    this.$router.openCaseDetail(row.id, {
      menuChannel: MENU_CHANNEL.Case_recover,
      hiddenCaseList: true,
      hiddenAction: true
    });
  }
  /**
   * 导出案件
   */
  private exportCaseInfo() {
    const ids = this.selectionList.map(v => v.id);
    const request: any = {
      ...this.importModel,
      caseIds: ids
    };
    this.CaseInfoService
      .recoveredCaseInfosExport(request)
      .subscribe(data => this.$common.downloadFile(data.url));
  }

  /**
   * 导出催记
   */
  private exportFollowup() {
    const ids = this.selectionList.map(v => v.id);
    const request: any = {
      ...this.importModel,
      caseIds: ids
    };
    this.CaseInfoService
      .recoveredCaseInfosExportFollow(request)
      .subscribe(data => this.$common.downloadFile(data.url));
  }
  /**
   * 更新回收方式
   */
  private updateRecoveryType() {
    if (this.selectionList.length !== 0) {
      const recoveryTypeArr : any = []
      this.selectionList.forEach((e)=>{
        if(e.recoveryType && e.recoveryType ==="PRINCIPAL_MANUAL_RECOVERY"){
          recoveryTypeArr.push(e.recoveryType);
        }
      });
      if(recoveryTypeArr.length !==0 ){
        return this.$message(`所选案件不能包含委托方回收案件！`);
      }
      const ids = this.selectionList.map(v => v.id);
      const request: any = {
        caseIds: ids
      };
      this.$confirm('确定更新所选案件为委托方回收吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        this.CaseInfoService
        .updateRecoveryType(request).subscribe(
          (data) => {
            this.$message.success('操作成功!');
            this.refreshData();
          },
          (err) => {
            this.$message.success('操作失败!');
          },
        );
      });
    } else {
      this.$message(`请选择要更新回收方式的案件`);
    }
  }
}
</script>

<style lang="less">
</style>
